//
//  ZH_ClassViewModel.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/11/9.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_ClassViewModel.h"
//课程分类
#define get_cate        @"/api/student/get_cate"
//年级分类
#define get_grade       @"/api/student/get_grade"
//线下，点播课程列表
#define get_course      @"/api/student/get_course"
//线下课程详情
#define course_detail   @"/api/student/course_detail"
//加入购物车
#define add_car         @"/api/student/add_car"
//用户购物车列表
#define studentCar      @"/api/student/car"
//提交订单 购物车结算
#define submit_order    @"/api/student/submit_order"
////删除购物车
#define del_car         @"/api/student/del_car"
////有课日期
#define MechanismDate   @"/api/Mechanism/date"
//支付宝支付
#define coursepayIndex  @"/api/coursepay/index"
@implementation ZH_ClassViewModel
//课程分类
- (void)studentGetcateResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:get_cate dictionaryParam:@{@"token":[DATAMODEL.configDBHelper getToken]} withResultBlock:^(NSString *code, NSString *message, id data) {
        NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
        if (![data[@"data"] isKindOfClass:[NSNull class]]) {
            resultArray = data[@"data"];
        }
        if(resultArray.count > 0){
            [self.classArray removeAllObjects];
            [self.classArray addObjectsFromArray:resultArray];
            if(resultBlock){
                resultBlock(code,message,data);
            }
        }
        else{
            if(resultBlock){
                resultBlock(code, message, nil);
            }
        }
    }];
}
//年级分类
- (void)studentGetgradeResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:get_grade dictionaryParam:@{@"token":[DATAMODEL.configDBHelper getToken]} withResultBlock:^(NSString *code, NSString *message, id data) {
        NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
        resultArray = data[@"data"];
        if(resultArray.count > 0){
            [self.gradeArray removeAllObjects];
            [self.gradeArray addObjectsFromArray:resultArray];
            if(resultBlock){
                resultBlock(code,message,data);
            }
        }
        else{
            if(resultBlock){
                resultBlock(code, message, nil);
            }
        }
    }];
}
//线下，点播课程列表
- (void)courseListResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:get_course dictionaryParam:self.courseListParam withResultBlock:^(NSString *code, NSString *message, id data) {
        NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
        if (![data[@"data"] isKindOfClass:[NSNull class]]) {
            resultArray = data[@"data"][@"data"];
        }
        if(resultArray.count > 0){
            //            [self.goodsRecommenderdData addObjectsFromArray:resultArray];
            if([[self.courseListParam objectForKey:@"page"] isEqualToString:@"1"]){
                [self.courseListArray removeAllObjects];
            }
            [self.courseListArray addObjectsFromArray:resultArray];
            if(resultBlock){
                resultBlock(code,message,self.courseListArray);
            }
        }
        else{
            if(resultBlock){
                resultBlock(code, message, nil);
            }
        }
    }];
}
//线下课程详情
- (void)courseDetailResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:course_detail dictionaryParam:self.courseDetailParam withResultBlock:^(NSString *code, NSString *message, id data) {
        if (resultBlock) {
            resultBlock(code,message,data);
        }
    }];
}
//加入购物车
- (void)addCarResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:add_car dictionaryParam:self.addCarParam withResultBlock:^(NSString *code, NSString *message, id data) {
        if(resultBlock){
            resultBlock(code,message,data);
        }
    }];
}
//提交订单 购物车结算
- (void)submitResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:submit_order dictionaryParam:self.submitParam withResultBlock:^(NSString *code, NSString *message, id data) {
        if (resultBlock) {
            resultBlock(code,message,data);
        }
    }];
}

//删除购物车
- (void)deleteCarResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:del_car dictionaryParam:self.deletCarParam withResultBlock:^(NSString *code, NSString *message, id data) {
        if (resultBlock) {
            resultBlock(code,message,data);
        }
    }];
}
//获取学生的订单列表
- (void)studentListResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:studentCar dictionaryParam:@{@"token":[DATAMODEL.configDBHelper getToken]} withResultBlock:^(NSString *code, NSString *message, id data) {
        NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
        [self.studentArray removeAllObjects];
        [self.studentListArray removeAllObjects];
        if (![data[@"data"] isKindOfClass:[NSNull class]]) {
             resultArray = data[@"data"][@"data"];
        }
        if(resultArray.count > 0){
            [self.studentArray addObjectsFromArray:resultArray];
            for (NSArray *array in resultArray) {
                for (NSDictionary *dic in array) {
                    [self.studentListArray addObject:[[ZH_StudentListModel alloc] initWithDictionary:dic]];
                }
            }
            if(resultBlock){
                resultBlock(code,message,data);
            }
        }
        else{
            if(resultBlock){
                resultBlock(code, message, nil);
            }
        }
    }];
}

//有课日期
- (void)dateListResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:MechanismDate dictionaryParam:self.dateParam withResultBlock:^(NSString *code, NSString *message, id data) {
        NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:0];
        if (![data[@"data"] isKindOfClass:[NSNull class]]) {
            resultArray = data[@"data"];
        }
        if(resultArray.count > 0){
            [self.dateArray removeAllObjects];
            [self.dateArray addObjectsFromArray:resultArray];
            if(resultBlock){
                resultBlock(code,message,data);
            }
        }
        else{
            if(resultBlock){
                resultBlock(code, message, nil);
            }
        }

    }];
}
//支付宝支付
- (void)applyResultBlock:(WBRequestResultBlock)resultBlock{
    [[WB_MessageManager shareMessageManager] requestPostWithAction:coursepayIndex dictionaryParam:self.applyParam withResultBlock:^(NSString *code, NSString *message, id data) {
        if (resultBlock) {
            resultBlock(code,message,data);
        }
    }];
}
#pragma mark - getter,setter
- (NSMutableArray *)classArray{
    if (!_classArray) {
        _classArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _classArray;
}
//年级分类
- (NSMutableArray *)gradeArray{
    if (!_gradeArray) {
        _gradeArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _gradeArray;
}
//线下，点播课程列表
- (NSMutableDictionary *)courseListParam{
    if (!_courseListParam) {
        _courseListParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_courseListParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
        [_courseListParam setObject:@"" forKey:@"cate"];
        [_courseListParam setObject:@"" forKey:@"grade"];
        [_courseListParam setObject:@"" forKey:@"way"];
        [_courseListParam setObject:@"" forKey:@"page"];
    }
    return _courseListParam;
}
- (NSMutableArray *)courseListArray{
    if (!_courseListArray) {
        _courseListArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _courseListArray;
}
//线下课程详情
- (NSMutableDictionary *)courseDetailParam{
    if (!_courseDetailParam) {
        _courseDetailParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_courseDetailParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
        [_courseDetailParam setObject:@"" forKey:@"id"];
    }
    return _courseDetailParam;
}
//加入购物车
- (NSMutableDictionary *)addCarParam{
    if (!_addCarParam) {
        _addCarParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_addCarParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
        [_addCarParam setObject:@"" forKey:@"course_id"];
    }
    return _addCarParam;
}
//获取学生的订单
- (NSMutableArray <ZH_StudentListModel *>*)studentListArray{
    if (!_studentListArray) {
        _studentListArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _studentListArray;
}
- (NSMutableArray *)studentArray{
    if (!_studentArray) {
        _studentArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _studentArray;
}
//提交订单 购物车结算
- (NSMutableDictionary *)submitParam{
    if (!_submitParam) {
        _submitParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_submitParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
        [_submitParam setObject:@"" forKey:@"course_id"];
    }
    return _submitParam;
}
//删除购物车
- (NSMutableDictionary *)deletCarParam{
    if (!_deletCarParam){
        _deletCarParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_deletCarParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
        [_deletCarParam setObject:@"" forKey:@"course_id"];
    }
    return _deletCarParam;
}
//有课日期
- (NSMutableDictionary *)dateParam{
    if (!_dateParam) {
        _dateParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_dateParam setObject:@"" forKey:@"id"];
        [_dateParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
    }
    return _dateParam;
}
- (NSMutableArray *)dateArray{
    if (!_dateArray) {
        _dateArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dateArray;
}
- (NSMutableDictionary *)applyParam{
    if (!_applyParam) {
        _applyParam = [[NSMutableDictionary alloc] initWithCapacity:0];
        [_applyParam setObject:[DATAMODEL.configDBHelper getToken] forKey:@"token"];
        [_applyParam setObject:@"" forKey:@"trade_no"];
        [_applyParam setObject:@"" forKey:@"pay_no"];
        [_applyParam setObject:@"" forKey:@"sign"];
    }
    return _applyParam;
}
@end
